//
//  FXEventHandle.swift
//  FXEvent
//
//  Created by Fanxx on 2021/11/23.
//

import Foundation
import BasicExtensions

/// 事件处理
public enum FXEventHandle<ParameterType>: Equatable {
    ///Target-Action
    case target(AnyObject, Selector)
    ///Block
    case block(String?, (ParameterType) -> Void)
    public static func == (lhs: FXEventHandle, rhs: FXEventHandle) -> Bool {
        switch lhs {
        case .target(let lObject, let lSelector):
            switch rhs {
            case .target(let rObject, let rSelector):
                return lObject === rObject && lSelector == rSelector
            case .block(_, _):
                return false
            }
        case .block(let lId, _):
            switch rhs {
            case .target(_, _):
                return false
            case .block(let rId, _):
                return lId == rId && lId != nil
            }
        }
    }
    ///Block的id
    public var identifier: String? {
        switch self {
        case .target(_, _):
            return nil
        case .block(let identifier, _):
            return identifier
        }
    }
    ///Target-Action的target
    public var target: AnyObject? {
        switch self {
        case .target(let object, _):
            return object
        case .block(_, _):
            return nil
        }
    }
    /// 执行操作
    public func execute(for parameter: ParameterType) {
        switch self {
        case .target(let anyObject, let selector):
            _ = anyObject.perform(selector, with: parameter)
//            doAction(for: anyObject, selector: selector, arg: parameter)
        case .block(_, let block):
            block(parameter)
        }
    }
}
/// 使用[FXEventHandle]来处理事件响应
public protocol FXEventHandleWithArray: AnyObject, FXEventType {
    var handles: [FXEventHandle<ParameterType>] { get set }
    var handleQueue: DispatchQueue { get }
}

extension FXEventHandleWithArray {
    /// 添加事件处理
    public func addTarget(_ target: AnyObject, action: Selector) {
        self.handleQueue.async {
            self.handles.append(.target(target, action))
        }
    }
    /// 移除事件处理
    public func removeTarget(_ target: AnyObject, action: Selector) {
        self.handleQueue.async {
            self.handles.removeAll(where: { $0 == .target(target, action)})
        }
    }
    /// 移除事件处理
    public func removeTarget(_ target: AnyObject) {
        self.handleQueue.async {
            self.handles.removeAll(where: { $0.target === target })
        }
    }
    /// 添加事件处理
    public func addBlock(_ identifier: String, block: @escaping (ParameterType) -> Void) {
        self.handleQueue.async {
            self.handles.append(.block(identifier,block))
        }
    }
    /// 添加事件处理
    public func addBlock(_ block: @escaping (ParameterType) -> Void) {
        self.handleQueue.async {
            self.handles.append(.block(nil, block))
        }
    }
    /// 移除事件处理
    public func removeBlock(_ identifier: String) {
        self.handleQueue.async {
            self.handles.removeAll(where: { $0.identifier == identifier })
        }
    }
    /// 移除全部事件处理
    public func removeAllActions() {
        self.handleQueue.async {
            self.handles.removeAll()
        }
    }
    /// 触发事件
    public func sendActions(for parameter: ParameterType) {
        if self.isEnabled {
            self.handles.forEach({ $0.execute(for: parameter) })
        }
    }
}
